import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'SingleTreeHolePage.dart';
import 'TreeHoleList.dart';

class MyTreeHolePage extends StatelessWidget{
  final String token;
  MyTreeHolePage(this.token);
  Widget build(BuildContext context){
    return Scaffold(
      appBar: AppBar(
        title: Container(
          child: Text('我的树洞',
            style: TextStyle(
                color: Colors.black,
                letterSpacing: 5
            ),
          ),
        ),
        centerTitle:true,
        backgroundColor: Colors.white,
        leading: IconButton(
          icon:Icon(Icons.arrow_back,color: Colors.black),
          onPressed: () {Navigator.pop(context);},
        ),
      ),
      body: Container(
        width: double.infinity,
        padding: EdgeInsets.all(15),
        child: ListView(
          children: [MyCard(this.token)],
        ),
        decoration: BoxDecoration(
            image: new DecorationImage(
                fit: BoxFit.cover,
                image: new AssetImage('images/treeHole.jpg')
            )
        ),
      ),
    );
  }
}

//轮播图
class MyCard extends StatefulWidget {
  final String token;
  MyCard(this.token,{Key key}) : super(key: key);

  @override
  _MyCard createState() => new _MyCard();
}
class _MyCard extends State<MyCard> {
  List myArticles;
  void initState() {
    super.initState();
    TreeHoleList.getMyArticles(widget.token).then((result) {
      setState(() {
        myArticles = result;
      });
    }).whenComplete(() {
      print("getMyArticles异步任务处理完成");
    });
    }
  @override
  Widget build(BuildContext context) {
    return myArticles!=null?ListView(
      children: myArticles.map((value){
        return InkWell(
          child: Card(
            margin: EdgeInsets.all(10),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ListTile(
                  title: Text(value['title'],
                    textAlign: TextAlign.center,
                  ),
                  subtitle: Text(value['content'],
                    maxLines: 7,
                    style: TextStyle(
                        fontSize: 15
                    ),
                  ),
                  isThreeLine: true,
                ),
                Row(children: [
                  Spacer(),
                  Padding(padding: EdgeInsets.only(right: 10,bottom: 10),child: Text(value['createTime']),)]),
              ],
            ),
          ),
          onTap: () async {
            var _article=await TreeHoleList.getArticle(value['id']);
            Navigator.push(context, new MaterialPageRoute(builder: (context) => SingleTreeHolePage(token:widget.token,article:_article)));
            },
          onLongPress: (){
            showDialog<Null>(
                context: context,
                barrierDismissible: true,
                builder: (BuildContext context) {
                  return new AlertDialog(
                    title: new Text('你确定要删除该树洞吗?', style: new TextStyle(fontSize: 17.0)),
                    actions: <Widget>[
                      new FlatButton(
                        child: new Text('取消'),
                        onPressed: (){
                          Navigator.of(context).pop();
                        },
                      ),
                      new FlatButton(
                        child: new Text('确定'),
                        onPressed: () async {
                          String result=await TreeHoleList.deleteArticle(widget.token,value['id']);
                          if(result=='删除成功'){
                            TreeHoleList.getMyArticles(widget.token).then((result) {
                              setState(() {
                                myArticles = result;
                              });
                            }).whenComplete(() {
                              print("getMyArticles再次异步任务处理完成");
                            });
                            Navigator.pop(context);
                          }
                          Fluttertoast.showToast(
                              msg: result,
                              toastLength: Toast.LENGTH_SHORT,
                              gravity: ToastGravity.BOTTOM,
                              timeInSecForIosWeb: 1,
                              backgroundColor: Colors.amber[50],
                              textColor: Colors.black,
                              fontSize: 16.0
                          );
                        },
                      )
                    ],
                  );
                }
            );
          },
        );
      } ).toList(),
      shrinkWrap: true,
      physics: new NeverScrollableScrollPhysics(),
    ):Align(alignment: Alignment.center,child: Text('空/加载中...'),);
  }
}